package com.gwz.wxtime.core.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gwz.wxtime.core.entity.base.BaseDTO;
import com.gwz.wxtime.core.entity.base.BaseV2DTO;
import com.gwz.wxtime.core.entity.base.WxResp;
import com.gwz.wxtime.core.entity.wx.AgreeToFriendRequest;
import com.gwz.wxtime.core.entity.wx.SendMessage;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class WxUtil {


    public static final String wxId = "wxid_szfr0q253d8y22";

    public static final String requestUrl = "http://127.0.0.1:9999/api";


    public static JSONObject sendMessage(String sendWxId, String message) {
  /*      BaseDTO<SendMessage> request = new BaseDTO<>();
        request.setType("Q0001");
        SendMessage sendMessage = new SendMessage();
        sendMessage.setWxid(sendWxId);
        sendMessage.setMsg(message);
        request.setData(sendMessage);*/
        BaseV2DTO baseV2DTO = new BaseV2DTO<>();
        baseV2DTO.setType(1);
        baseV2DTO.setWxid(sendWxId);
        baseV2DTO.setMsg(message);


        if (message.contains("{@")) {

            Pattern c = Pattern.compile("\\{@([^}]*)@\\}");
            Matcher matcher = c.matcher(message);

            List<String> list = new ArrayList<>();

            while (matcher.find()) {
                list.add(matcher.group().substring(2, matcher.group().length() - 2));
            }

            if (CollUtil.isNotEmpty(list)) {
                baseV2DTO.setType(5);
                baseV2DTO.setChatroomid(sendWxId);
                baseV2DTO.setList(list);
                for (String s : list) {
                    message =  message.replaceAll("\\{@" + s + "@\\}", "");
                }
                baseV2DTO.setMsg(message);
            }


            // baseV2DTO.setList(sendWxId);

        }

        return post(baseV2DTO);
    }


    public static JSONObject sendImage(String sendWxId, String path) {
     /*   BaseDTO<SendMessage> request = new BaseDTO<>();
        request.setType("Q0010");
        SendMessage sendMessage = new SendMessage();
        sendMessage.setWxid(sendWxId);
        sendMessage.setPath(path);
        request.setData(sendMessage);*/

        BaseV2DTO baseV2DTO = new BaseV2DTO<>();
        baseV2DTO.setType(2);
        baseV2DTO.setWxid(sendWxId);
        baseV2DTO.setImagepath(path);
        return post(baseV2DTO);
    }

    public static JSONObject sendAgreeToFriendRequest(String v3, String v4, String scene) {
    /*    BaseDTO<AgreeToFriendRequest> request = new BaseDTO<>();
        request.setType("Q0017");
        AgreeToFriendRequest agreeToFriendRequest = new AgreeToFriendRequest();
        agreeToFriendRequest.setV3(v3);
        agreeToFriendRequest.setV4(v4);
        agreeToFriendRequest.setScene(scene);
        request.setData(agreeToFriendRequest);
*/

        BaseV2DTO request = new BaseV2DTO<>();
        request.setType(11);
        request.setV3(v3);
        request.setV4(v4);
        return post(request);
    }

    public static String getNickName(String wxid, String formWxid) {

 /*       BaseDTO<SendMessage> request2 = new BaseDTO<>();
        request2.setType("Q0004");
        SendMessage sendMessage2 = new SendMessage();
        sendMessage2.setWxid(wxid);
        request2.setData(sendMessage2);
        WxResp post = postNoDealy(request2);
        JSONObject result = (JSONObject) post.getResult();*/
        BaseV2DTO request = new BaseV2DTO<>();
        request.setType(50);
        request.setChatroomid(formWxid);
        request.setWxid(wxid);
        JSONObject jsonObject = postNoDealy(request);

        return jsonObject.getString("nickname");
    }

    public static synchronized JSONObject postNoDealy(BaseV2DTO request) {
        log.info("hook发送消息:{}", JSON.toJSONString(request));
        String result = HttpRequest.post(requestUrl + "?wxid=" + wxId)
                .body(JSON.toJSONString(request))
                .execute()
                .body();
        log.info("hook响应消息:{}", result);
        JSONObject wxResp = JSON.parseObject(result);
        return wxResp;
    }

    public static synchronized JSONObject post(BaseV2DTO request) {
        log.info("hook发送消息:{}", JSON.toJSONString(request));
        String result = HttpRequest.post(requestUrl + "?wxid=" + wxId)
                .body(JSON.toJSONString(request))
                .execute()
                .body();
        log.info("hook响应消息:{}", result);
        JSONObject wxResp = JSON.parseObject(result);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return wxResp;
    }


    public static String at(String wxId) {
        return "{@" + wxId + "@} ";
    }

}
